import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * The nth term of the sequence of triangle numbers is given by tn = ½n(n+1); 
 * so the first ten triangle numbers are: 1, 3, 6, 10, 15, 21, 28, 36, 45,...
 * By converting each letter in a word to a number corresponding to 
 * its alphabetical position and adding these values we form a word value. 
 * For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. 
 * If the word value is a triangle number 
 * then we shall call the word a triangle word. 
 * Using words.txt (right click and 'Save Link/Target As...'), 
 * a 16K text file containing nearly two-thousand common English words, 
 * how many are triangle words?
 */

/**
 * @author TrinhNX
 * @start_date	: 2015/05/20
 * @end_date 	:
 */
public class Euler042 {

	/**
	 * @author		: TrinhNX
	 * @param args
	 * @start_date	: 2015/05/20
	 * @end_date	:
	 */
	public static void main(String[] args) {
		// Well, straightforward mind
		// NOTE1: Name file is in upper case
		// 1. Read the file, get the name -> array :-? ok.
		// 2. Loop to calculate the name value
		// 3. Check name value is triangle value
		// - small function to get value from char
		// Well well, idea to optimize 
		// 1. Array the value, sort
		// 2. It is limited the name value
		// It is quicker to look from the value than the name :v 
		// LOL: run first, 13ms (first)

		// NOTE 2; Using the sort search value, hmm, reduce to 12 (average)
		// NOTE 3: well, it should be only limited :v ;v
		// Using hashmap :D :-? Well, it looks the same :( 10ms
		int count = 0;
		final long start;
		try {
			final String[] split_result = Common.readFile("p042_words.txt").split(",");
			start = System.currentTimeMillis();
			//			for(String data: split_result) {
			//				if(TRIANGLE_MAP.containsKey(triangeValFromString(data))) {
			//					count++;
			//				}
			//			}
			final int LENGTH = split_result.length;
//			final int[] temp_result = new int[LENGTH];
			int temp_val, temp_sum, temp_square;
			for (int i = 0; i < LENGTH; i++) {
				//				temp_result[i] = triangeValFromString(split_result[i]);
				temp_sum = triangeValFromString(split_result[i]);
				temp_val = 8 * temp_sum + 1;
				temp_square = (int) Math.sqrt(temp_val);
				if ((temp_val - temp_square * temp_square) == 0)
					count++;

				//				if (Arrays.binarySearch(TRIANGLE_ARRAY, triangeValFromString(data)) >= 0) {
				//					count++;
				//				}
			}
			//			Arrays.sort(temp_result);
			//			for (int i = 0; i < MAX; i++) {
			//				if (Arrays.binarySearch(temp_result, TRIANGLE_ARRAY[i]) >= 0)
			//					count++;
			//			}

		} catch (IOException e) {
			System.err.println("Error reading file");
			return;
		}
		System.out.println(count);
		System.out.println("End after: " + (System.currentTimeMillis() - start));
	}

	// Just used for this problem, the string source contain ""
	private static int triangeValFromString(final String source) {
		int result = 0;
		for (int i = 1, length = source.length() - 1; i < length; i++) {
			result += source.charAt(i) - 64;
		}
		return result;
	}

	static final int MAX = 30;
	static final int[] TRIANGLE_ARRAY = new int[MAX];
	static final Map<Integer, String> TRIANGLE_MAP = new HashMap<Integer, String>(MAX);
	static {
		for (int i = 0; i < MAX; i++) {
			TRIANGLE_MAP.put((i + 2) * (i + 1) / 2, "available");
		}
	}

}
